package controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

import controller.UserInputControl;
import dataStructure.LinkedList;
import entity.FileAsso;
import entity.Staff;

public class LoginController {
	private LinkedList<Staff> staffList = new LinkedList<Staff>();
	private Staff staff;
	private boolean isLogged;

	public LoginController() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NullPointerException, ParseException {
		FileIOController fileIO = new FileIOController();
		
		fileIO.getFileAssoList().addContent(new FileAsso("staff", "staff.txt"));
		
		fileIO.inputData(staffList, "staff");
	}

	public boolean login() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NullPointerException {
		if(staffList.getFirstNode()!=null){
			System.out.println("Login in order to start up CMMS.");
			String username = UserInputControl.getStrInput("User name: ", "[a-zA-Z0-9 ]+");
			String pwd = UserInputControl.getStrInput("Password: ", "[a-zA-Z0-9 ]+");
	
			if(username != null && pwd != null){
				Staff result = this.staffList.searchContent("staffName", username);
				if (result != null) {
					if (result.getPassword().equals(pwd)) {
						this.setStaff(result);
						isLogged=true;
						return true;
					}
				}
			}
		} else {
			System.out.println("No data is loaded to staffList. Contact Technicians for fixing this problem: staff.txt is not found.");
		}
		return false;
	}

	public LinkedList<Staff> getStaffList() {
		return staffList;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public Staff getStaff() {
		return staff;
	}

	public boolean isLogged() {
		return isLogged;
	}

	public void setLogged(boolean isLogged) {
		this.isLogged = isLogged;
	}
}
